package controller;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.NumberFormatter;

import model.KhachHang;

public class KhachHangController {

	public static KhachHang getKHachHang(int id) {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM khachhang WHERE id=" + id + ";";
			ResultSet rs = stmt.executeQuery(sql);
			KhachHang kh;
			if (rs.next()) {
				kh = new KhachHang();
				kh.setMaKH(id);
				kh.setTenKH(rs.getString("tenkh"));
				kh.setDiaChi(rs.getString("diachi"));
				kh.setSdt(rs.getString("sdt"));
				kh.setDiemTichLuy(rs.getDouble("diemtichluy"));
				return kh;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static List<KhachHang> getDSKhachHang() {
		Connection conn = DBConection.MYSQL;
		if (conn == null)
			return null;
		try {

			Statement stmt = conn.createStatement();
			String sql = "SELECT * FROM khachhang";
			ResultSet rs = stmt.executeQuery(sql);
			List<KhachHang> list = new ArrayList<>();
			KhachHang kh;
			while (rs.next()) {
				kh = new KhachHang();
				kh.setMaKH(rs.getInt("id"));
				kh.setTenKH(rs.getString("tenkh"));
				kh.setDiaChi(rs.getString("diachi"));
				kh.setSdt(rs.getString("sdt"));
				// double d = rs.getDouble("diemtichluy");
				// DecimalFormat decimalFormat = new DecimalFormat("d");
				// NumberFormatter textFormatter = new
				// NumberFormatter(decimalFormat);
				// double t = Double.toString(textFormatter);
				kh.setDiemTichLuy(rs.getDouble("diemtichluy"));
				list.add(kh);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		}
	}

	public static void insertKhachHang(KhachHang kh) {

		try {
			Statement sta = DBConection.MYSQL.createStatement();
			String query = "INSERT INTO khachhang (id,tenKH,diachi,sdt,diemtichluy) VALUES (";
			query += "'" + kh.getMaKH() + "',";
			query += "'" + kh.getTenKH() + "',";
			query += "'" + kh.getDiaChi() + "',";
			query += "'" + kh.getSdt() + "'," + "'" + kh.getDiemTichLuy();
			query += "')";
			System.out.println(query);
			sta.execute(query);
			return;

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public static void updateKhachHang(KhachHang kh) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "UPDATE khachhang SET " + "tenkh=" +"'"+ kh.getTenKH()
					+ "'"+"," + "diachi= " +"'"+ kh.getDiaChi() +"'"+ "," + "sdt="
					+ "'"+kh.getSdt() +"'"+ "," + "diemtichluy= " +"'"+ kh.getDiemTichLuy()
					+ "'"+" WHERE id =" + kh.getMaKH();
			System.out.println(query);
			st.execute(query);
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	public static void deleteKhachHang(int id) {
		try {
			Statement st = DBConection.MYSQL.createStatement();
			String query = "DELETE FROM khachhang WHERE id = "+ id;
			st.execute(query);
			return;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	public static void main(String[] args) {
		KhachHangController c = new KhachHangController();
		System.out.println(c.getDSKhachHang().size());
//		c.updateKhachHang(new KhachHang(1, "Lee", "lee", "810414", 0));

	}
}
